﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GMAC.ReportingSystem.Paginas
{
    public partial class GeneralParameters : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated || Session["User"] == null)
            {
                System.Web.Security.FormsAuthentication.RedirectToLoginPage();
                return;
            }

            if (!this.IsPostBack)
            {
                try
                {
                    Business.Components.GeneralParameterComponent bc = new GMAC.ReportingSystem.Business.Components.GeneralParameterComponent();
                    this.ConfigurationControl1.DataSource = bc.GetConfigurationValues();
                    this.ConfigurationControl1.DataBind();
                }
                catch (Exception ex)
                {
                    FrameworkManapro.ErrorHandling.LoggingError.PostErrorEventViewer(ex.Message, "Load general parameters", "Page_Load", System.Diagnostics.TraceEventType.Error, ex.StackTrace);
                    this.wucMessage.ShowMessage(UserControls.MessageType.Error, "An error has occured.", ex.Message);
                    this.popupExtenderMessage.Show();
                }
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            Business.Components.GeneralParameterComponent bc = new GMAC.ReportingSystem.Business.Components.GeneralParameterComponent();
            Business.Entities.Configuration.ConfigurationParameterCollection values = this.ConfigurationControl1.DataSource as Business.Entities.Configuration.ConfigurationParameterCollection;

            if (values != null)
            {
                try
                {
                    if (bc.SetConfigurationValues(values))
                    {
                        this.wucMessage.ShowMessage(UserControls.MessageType.Success, "The general parameters were successfully saved.", null);
                        this.popupExtenderMessage.Show();
                    }
                    else
                    {
                        this.wucMessage.ShowMessage(UserControls.MessageType.Error, "The general parameters couldn't be saved.", null);
                        this.popupExtenderMessage.Show();
                    }
                }
                catch (Exception ex)
                {
                    FrameworkManapro.ErrorHandling.LoggingError.PostErrorEventViewer(ex.Message, "Save general parameters", "btnSave_Click", System.Diagnostics.TraceEventType.Error, ex.StackTrace);
                    this.wucMessage.ShowMessage(UserControls.MessageType.Error, "An error has occured.", ex.Message);
                    this.popupExtenderMessage.Show();
                }
            }
        }
    }
}
